package com.neusoft.lohas.modules.shop.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.neusoft.lohas.common.config.Global;
import com.neusoft.lohas.common.pay.PayBase;
import com.neusoft.lohas.common.web.BaseShopController;
import com.neusoft.lohas.modules.ivm.entity.order.Order;
import com.neusoft.lohas.modules.ivm.enums.sys.MessageEnum;
import com.neusoft.lohas.modules.shop.service.ShopService;
import com.neusoft.lohas.modules.sys.entity.User;
import com.neusoft.lohas.modules.sys.utils.UserUtils;

@Controller
@RequestMapping(value = "${frontPath}/shops/myorder")
public class MyOrderController extends BaseShopController{

	@Autowired
	private ShopService shopService;
	
	/**
	 * 跳转到我的订单主页
	 * @param mobile
	 * @param username
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "")
	public String showMyOrderPage(
			String mobile,
			String username,
			@RequestParam(value="appId", required=false, defaultValue="")String appId,
			HttpServletRequest request, HttpServletResponse response,RedirectAttributes redirectAttributes, Model model){
		//处理mobile参数
		if(StringUtils.isNotBlank(mobile))mobile = mobile.split(",")[0];
		User user = (User) UserUtils.getShopUser(mobile, username);
//		model.addAttribute("user", user);
//		Order order = new Order();
//		order.setUser(user);
//		List<Long> list = Lists.newArrayList();
//		list.add(shopService.findMyOrderCount(order, 1));
//		list.add(shopService.findMyOrderCount(order, 2));
//		list.add(shopService.findMyOrderCount(order, 3));
//		list.add(shopService.findMyOrderCount(order, 4));
//		list.add(shopService.findMyOrderCount(order, 5));
//		list.add(shopService.findMyOrderCount(order, 6));
//		model.addAttribute("list", list);
		return "redirect:"+Global.getFrontPath()+"/shops/myorder/list?userId="+user.getId()+"&type=1&appId="+appId;
//		return "mobile/shop/myorder/index";
	}
	
	@RequestMapping(value = "list")
	public String searchOrderList(
			String userId,
			@RequestParam(value="type", required=true, defaultValue="1")int type,
			@RequestParam(value="appId", required=false, defaultValue="")String appId,
			HttpServletRequest request, HttpServletResponse response, Model model){
		Order order = new Order();
		order.setUser(new User(userId));
		List<Order> orders = shopService.findMyOrderList(order, type);
		User user = UserUtils.getUserById(order.getUser().getId());
		List<Long> list = Lists.newArrayList();
		list.add(shopService.findMyOrderCount(order, 1));
		list.add(shopService.findMyOrderCount(order, 2));
		list.add(shopService.findMyOrderCount(order, 3));
		list.add(shopService.findMyOrderCount(order, 4));
		list.add(shopService.findMyOrderCount(order, 5));
		list.add(shopService.findMyOrderCount(order, 6));
		list.add(shopService.findMyOrderCount(order, 7));
		model.addAttribute("list", list);
		model.addAttribute("orders", orders);
		model.addAttribute("user", user);
		model.addAttribute("userId", userId);
		model.addAttribute("type", type);
		model.addAttribute("appId", appId);
		return "mobile/shop/myorder/orderList";
	}
	
	/**
	 * 订单详情
	 * @param orderNo
	 * @param sid
	 * @param mobile
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "order/detail")
	public String orderDetail(
			String orderNo, 
			String userId,
			@RequestParam(value="type", required=true, defaultValue="1")int type,
			@RequestParam(value="appId", required=false, defaultValue="")String appId,
			HttpServletRequest request, HttpServletResponse response, Model model){
		User user = UserUtils.getUserById(userId);
		Order order = shopService.findByOrderNo(orderNo);
		if (order==null) {
			return "redirect:"+Global.getFrontPath()+"/shops/sysAlert?code="+MessageEnum.NOT_EXISTS_ORDER.getCode();
		}
		model.addAttribute("order", order);
		model.addAttribute("user", user);
		model.addAttribute("type", type);
		model.addAttribute("appId", appId);
		model.addAttribute("mobile", user.getMobile());
		return "mobile/shop/myorder/orderDetail";
	}
	
	@RequestMapping(value = "order/toPay")
	public String orderToPay(
			String orderNo, 
			String userId, 
			@RequestParam(value="appId", required=false, defaultValue="")String appId,
			HttpServletRequest request, HttpServletResponse response, Model model){
		User user = UserUtils.getUserById(userId);
		Order order = shopService.findByOrderNo(orderNo);
		if (order==null) {
			return "redirect:"+Global.getFrontPath()+"/shops/sysAlert?code="+MessageEnum.NOT_EXISTS_ORDER.getCode();
		}
		if (order.getPayType().equals("2")) { //支付宝
			String redirectPage = PayBase.alipaySubmit_MW(order, user.getMobile(), user.getName(), "", "", "", 1);
			return "redirect:"+redirectPage;
		}else if (order.getPayType().equals("3")) { //银联
			String redirectPage = PayBase.unionpaySubmit_MW(request, order, user.getMobile(), user.getName(), "", "", "", 1);
			return "redirect:"+redirectPage;
		}
		return null;
	}
}
